<template>
  <el-dialog title="新建任务" :visible="localIsShowNewTask" width="40%" :style="{ height: '80vh' }" @open="onOpened" @close="onClose">
    <el-form ref="form" label-position="left" :rules="taskCreateRules" :model="form" label-width="80px">
      <el-form-item label="任务名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="文件名称" prop="fileName">
        <el-input v-model="form.fileName"></el-input>
      </el-form-item>
      <el-form-item label="文件类型" prop="fileSuffix">
        <el-select v-model="form.fileSuffix" placeholder="请选则导出类型" style="width: 155px;">
          <el-option label="XLSX" value=".xlsx"></el-option>
          <el-option label="CSV" value=".csv"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">立即创建</el-button>
        <el-button @click="localIsShowNewTask = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
const dayjs = require('dayjs');

export default {
  props: {
    taskType: {
      type: String,
      default: ''
    }
  },
  watch:{
    taskType(newVal) {
      this.form.type = newVal
      this.form.taskParam = newVal
    },
  },
  created() {
  },
  data() {
    return {
      taskCreateRules: {
        name: [
          {required: true, message: '请输入任务名称', trigger: 'blur'}
        ],
        fileName: [
          {required: true, message: '请输入文件名称', trigger: 'blur'}
        ],
        fileSuffix: [
          {required: true, message: '请选择文件类型', trigger: 'blur'}
        ]
      },
      localIsShowNewTask: false,
      form: {
        name: '',
        fileName: '',
        fileSuffix: '.xlsx',
        type: this.taskType,
        taskParam: this.taskType
      }
    };
  },
  methods:{
    onSubmit() {
      this.form.type = this.form.type + "-export"
      this.$http.post(this.$constant.baseURL + `/offline-export/create-task`, this.form, true)
        .then(() => {
          this.$message({
            message: "任务创建成功!",
            type: "success"
          });

          this.doClose()
        })
        .catch((error) => {
          this.$message({
            message: error,
            type: "error"
          });
        });
    },

    doClose(){
      this.localIsShowNewTask = false;
    },

    doOpen(){
      this.localIsShowNewTask = true;
    },

    onOpened(){
      const formattedDate = dayjs(Date.now()).format('YYYY-MM-DD HH:mm:ss.SSS');
        this.form = {
          name: 'export-' + this.taskType + '-' + formattedDate,
          fileName: 'file-' + this.taskType + '-' + formattedDate,
          fileSuffix: '.xlsx',
          type: this.taskType,
          taskParam: this.taskType
        }
    },

    onClose(){
      this.localIsShowNewTask = false;
      this.form = {
          name: '',
          fileName: '',
          fileSuffix: '.xlsx',
          type: this.taskType,
          taskParam: this.taskType
        }
    }
  }
};
</script>

<style scoped>

</style>
